<!--
 * @Author: your name
 * @Date: 2021-11-25 09:39:51
 * @LastEditTime: 2021-12-03 18:42:49
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \li-yuwei\client\src\view\Archived.vue
-->
<template>
  <div class="archived">
    <div class="left-box">
      <HintCom
        title=""
        :text="$t('archives')"
        :prefix="$t('total')"
        :num="num"
        :piece="$t('count')"
        style="border: none"
      >
        <!-- body插槽 -->
        <template slot="body">
          <div class="hint-body" v-for="item in allArticle" :key="item.id">
            <div class="year-hint">{{ item.year }}</div>
            <div
              class="month-hint"
              v-for="mItem in item.monthList"
              :key="mItem.id"
            >
              {{ mItem.month | EnglishDate }}
              <div
                class="date-hint"
                v-for="v in mItem.data"
                :key="v.id"
                @click="toArticleDetail(v.id)"
              >
                <span>
                  <i
                    class="iconfont icon-yuandian"
                    style="font-size: 12px; color: #ccc"
                  ></i
                  >{{ v.date }}</span
                >
                <b>{{ v.title }}</b>
              </div>
            </div>
          </div>
        </template>
      </HintCom>
      <!-- <el-card style="border: none; border-radius: 0" shadow="never">1</el-card> -->
    </div>
    <div class="right-box">
      <Recommend
        :data="recommendArticle"
        :handlerClick="toArticleDetail"
        :title="$t('recommend_title')"
      />
      <ClassifyCom
        :data="classifyList"
        :handlerClick="toClassifyDetail"
        :title="$t('article_cate')"
      />
    </div>
  </div>
</template>

<script>
import api from "../api";
import getDateDiff from "@/utils/getDateDiff.js";
import HintCom from "../components/HintCom.vue";
import Recommend from "../components/Recommend.vue";
import ClassifyCom from "../components/ClassifyCom.vue";
import handlerDate from "../utils/handlerDate";
import month from "../data/dateMonth";
export default {
  components: {
    HintCom,
    Recommend,
    ClassifyCom,
  },
  data() {
    return {
      num: 0,
      recommendArticle: [],
      classifyList: [],
      allArticle: [],
    };
  },
  filters: {
    EnglishDate(value) {
      return month[value * 1 - 1];
    },
  },
  methods: {
    //初始化函数
    async init() {
      await this.getAllArticle();
      await this.getRecommendArticle();
      this.getClassifyNum();
      this.handlerList();
    },
    //获取全部文章
    async getAllArticle() {
      const res = await api.getStateArticle();
      if (res.success) {
        this.allArticle = res.data;
        this.allArticle = this.allArticle.map((item) => {
          return {
            ...item,
            date_time: item.release_time,
            release_time: getDateDiff(new Date(item.release_time).getTime()),
            year: item.release_time.split("-")[0],
            month: item.release_time.split("-")[1],
            date:
              item.release_time.split("-")[1] +
              "-" +
              item.release_time.split("-")[2].split(" ")[0],
          };
        });
        this.num = this.allArticle.length;
      } else {
        this.$message({
          type: "warning",
          message: "意外的错误",
        });
      }
    },
    async getRecommendArticle() {
      const res = await api.recommendArticle();
      this.recommendArticle = res.data;
      this.recommendArticle = this.recommendArticle.map((item) => {
        return {
          ...item,
          release_time: getDateDiff(new Date(item.release_time).getTime()),
        };
      });
    },
    //分类各个数量
    async getClassifyNum() {
      const res = await api.getClassifyNum();
      if (res.success) {
        this.classifyList = res.data;
        this.classifyList = this.classifyList.map((item) => {
          if (item.num) return item;
          return { ...item, num: 0 };
        });
        this.classifyList.sort((a, b) => {
          return b.num - a.num;
        });
      } else {
        this.$message({
          type: "warning",
          message: "意外的错误",
        });
      }
    },
    //跳转文章详情
    toArticleDetail(id) {
      this.$router.push({
        path: `/article/${id}`,
      });
    },
    //跳转文章详情
    toClassifyDetail(value) {
      this.$router.push({
        path: `/classify/${value}`,
      });
    },
    handlerList() {
      console.log(this.allArticle);
      const res = handlerDate(this.allArticle);
      this.allArticle = res;
      console.log(res);
      2;
    },
  },
  created() {
    this.init();
  },
};
</script>

<style scoped lang="scss">
@import "@/assets/scss/common/common.scss";
.archived {
  width: 100%;
  .left-box {
    width: 70%;
  }
  .right-box {
    width: 28%;
  }
}
.hint-body {
  text-align: left;
  padding: 10px;
  padding-left: 20px;
}
.year-hint {
  font-size: 26px;
}
.month-hint {
  padding-left: 20px;
  font-size: 16px;
  @include font_color("text-color");
  margin: 10px 0;
  > div {
    cursor: pointer;
    font-size: 14px;
    padding-left: 20px;
    margin: 10px 0;
    span {
      margin-right: 10px;
      font-size: 12px;
      @include font_color("text-color");

      .iconfont {
        margin-right: 10px;
      }
    }
    b {
      @include font_color("text-color");
    }
  }
  > div:hover {
    color: #ff0071 !important;
    span {
      color: #ff0071 !important;
    }
    b {
      color: #ff0071 !important;
    }
    .iconfont {
      color: #ff0071 !important;
    }
  }
}
</style>
